package de.listrong.da.maerial;

import com.racersystems.jracer.RacerClientException;
import com.racersystems.jracer.RacerClient;

import java.io.File;

/**
 * Created by IntelliJ IDEA.
 * User: Qiang
 * Date: 12.11.2008
 * Time: 22:22:02
 * 
 * This class reads owl files from a folder and uses them to populate a triple store.
 * @author Atila
 */
public class TSPopulater {

    public TSPopulater(String folderPath, String tsName, String tsFolder) throws RacerClientException {

        String host = "localhost";
        int socket = 9999;
        RacerClient racer = new RacerClient(host, socket);
        //racer.setPrintWarningMessages(true);
        populateTS(folderPath, tsName, tsFolder, racer);
    }

    private void populateTS(String folderPath, String tsName, String tsFolder, RacerClient racer) throws RacerClientException {

        System.out.println("STARTING POPULATION");

        racer.openConnection();

        racer.send("(full-reset)");

        racer.send("(create-triple-store \"" + tsName + "\" :directory \"" + tsFolder + "\"  :if-exists :supersede)");

        File folder = new File(folderPath);

        String[] fileNames = folder.list();

        for (int i = 0; i < fileNames.length; i++) {

            if (!fileNames[i].startsWith(".")) {
                racer.send("(open-triple-store \"" + tsName + "\" :directory \"" + tsFolder + "\")");
                System.out.println("adding " + fileNames[i] + " into the triple store");
                long start = System.currentTimeMillis();
                racer.send("(triple-store-read-file \"" + folderPath + "/" + fileNames[i] + "\" :init nil :ignore-import t :index-p nil)");
                System.out.println("i) mili-seconds spent in triple-store-read-file " + (System.currentTimeMillis() - start));

                System.out.println("adding inherent triples for " + fileNames[i] + " into the triple store");

                racer.send("(owl-read-document \"file://E:/ontologies/MedOnto.owl\" :kb-name medOnt)");  // tbox
//                racer.send("(owl-read-document \"file:///Users/atila/Desktop/mco.owl\" :kb-name boemie :init nil)");
//                racer.send("(owl-read-document \"file:///Users/atila/Desktop/gio.owl\" :kb-name boemie :init nil)");
                racer.send("(owl-read-document \"file://" + folderPath + "/" + fileNames[i] + "\" :kb-name medOnt :init nil)");
                start = System.currentTimeMillis();
                racer.send("(materialize-inferences medOnt :db \"" + tsName + "\" :directory \"" + tsFolder + "\" :abox t :index-p nil)");
                System.out.println("ii) mili-seconds spent in materialize-inferences " + (System.currentTimeMillis() - start));
                racer.send("(full-reset)");
            }
        }

        System.out.println("POPULATION FINISHED");

        System.out.println("INDEXING THE TRIPLE STORE");

        racer.send("(open-triple-store \"" + tsName + "\" :directory \"" + tsFolder + "\")");

        racer.send("(index-all-triples)");

        racer.send("(close-triple-store)");

        System.out.println("INDEXING FINISHED");

        racer.closeConnection();
    }
}
